﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;
using MovieBrowser.Util;

namespace MovieBrowser.Presentation.Forms.Dialogs
{
    public partial class ColorSetup : Form
    {
        public static readonly Color[] HardnessBackground = new[] { Color.White, Color.FromArgb(123, 82, 161), Color.FromArgb(56, 82, 166), Color.FromArgb(77, 133, 132), Color.FromArgb(105, 189, 68), Color.FromArgb(180, 211, 55), Color.FromArgb(246, 235, 19), Color.FromArgb(244, 189, 28), Color.FromArgb(247, 143, 30), Color.FromArgb(242, 99, 33), Color.FromArgb(237, 32, 37), };
        public static readonly Color[] HardnessForeground = new[] { Color.Black, Color.White, Color.White, Color.White, Color.White, Color.Navy, Color.Black, Color.Black, Color.White, Color.White, Color.White, };

        private readonly List<Label> _labels = new List<Label>();

        public ColorSetup()
        {
            InitializeComponent();

            var font = Properties.Settings.Default.ListFont;

            for (int i = 0; i < ComponentUtility.HardnessBackground.Length; i++)
            {
                var backColor = ComponentUtility.HardnessBackground[i];
                var foreColor = ComponentUtility.HardnessForeground[i];

                var lbl = new Label
                              {
                                  Text = string.Format(textBox1.Text + " {0}", i),
                                  BackColor = backColor,
                                  ForeColor = foreColor,
                                  Dock = DockStyle.Fill,
                                  TextAlign = ContentAlignment.MiddleCenter,
                                  Font = font,
                              };
                _labels.Add(lbl);
                tableLayoutPanel1.Controls.Add(lbl, 0, i);

                var buttonForeColor = new Button
                                          {
                                              Text = string.Format("ForeColor {0}", i),
                                              Tag = lbl,
                                              Dock = DockStyle.Fill
                                          };
                buttonForeColor.Click += ButtonForeColorClick;
                tableLayoutPanel1.Controls.Add(buttonForeColor, 1, i);

                var buttonBackColor = new Button
                                          {
                                              Text = string.Format("BackColor {0}", i),
                                              Tag = lbl,
                                              Dock = DockStyle.Fill
                                          };
                buttonBackColor.Click += ButtonBackColorClick;
                tableLayoutPanel1.Controls.Add(buttonBackColor, 2, i);

            }
        }

        void ButtonBackColorClick(object sender, EventArgs e)
        {
            var lbl = (Label)((Button)sender).Tag;
            colorDialog1.Color = lbl.BackColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                lbl.BackColor = colorDialog1.Color;
            }
        }

        void ButtonForeColorClick(object sender, EventArgs e)
        {
            var lbl = (Label)((Button)sender).Tag;
            colorDialog1.Color = lbl.ForeColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {

                lbl.ForeColor = colorDialog1.Color;
            }
        }




        private void Button4Click(object sender, EventArgs e)
        {
            try
            {
                if (fontDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    foreach (var label in _labels)
                    {
                        label.Font = fontDialog1.Font;
                    }
                    textBox1.Font = fontDialog1.Font;
                }
            }
            catch
            {
            }
        }

        private void ColorSetupLoad(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.Foregrounds != null)
            {
                for (int i = 0; i < Properties.Settings.Default.Foregrounds.Count; i++)
                {
                    var foreground = Properties.Settings.Default.Foregrounds[i];
                    var background = Properties.Settings.Default.Backgrounds[i];
                    try
                    {
                        _labels[i].ForeColor = ColorTranslator.FromHtml(foreground);
                        _labels[i].BackColor = ColorTranslator.FromHtml(background);
                    }
                    catch
                    {
                    }
                }
            }
            textBox1.Font = Properties.Settings.Default.ListFont;
            fontDialog1.Font = Properties.Settings.Default.ListFont;
        }

        private void Button1Click(object sender, EventArgs e)
        {
            for (int i = 0; i < _labels.Count; i++)
            {
                var label = _labels[i];
                label.Text = string.Format(textBox1.Text + " {0}", i);
            }
        }

        private void ButtonSaveSettingsClick(object sender, EventArgs e)
        {
            Properties.Settings.Default.Foregrounds = new StringCollection();
            Properties.Settings.Default.Backgrounds = new StringCollection();
            foreach (var label in _labels)
            {
                string forecolor = ColorTranslator.ToHtml(label.ForeColor);
                string backcolor = ColorTranslator.ToHtml(label.BackColor);
                Properties.Settings.Default.Foregrounds.Add(forecolor);
                Properties.Settings.Default.Backgrounds.Add(backcolor);
            }
            Properties.Settings.Default.ListFont = textBox1.Font;
            Properties.Settings.Default.Save();
        }

        private void ButtonResetClick(object sender, EventArgs e)
        {
            for (int i = 0; i < _labels.Count; i++)
            {
                var label = _labels[i];
                label.ForeColor = HardnessForeground[i];
                label.BackColor = HardnessBackground[i];
                label.Font = Properties.Settings.Default.ListFont;
            }
            textBox1.Font = Properties.Settings.Default.DefaultListFont;
        }
    }
}
